package com.smt.countDownLatch;

import java.util.concurrent.CountDownLatch;

/**
 * @autor xiaoyu.fang
 * @date 2019/9/24 10:44
 */
public class LatchDemo implements Runnable {

    private CountDownLatch latch;

    public LatchDemo(CountDownLatch latch) {
        this.latch = latch;
    }

    @Override
    public void run() {
        synchronized (this) {
            try {
                // 打印50000内的偶数
                for (int i = 0; i < 50000; i++) {
                    if (i % 2 == 0) {
                        System.out.println(i);
                    }
                }
            } finally {
                // 线程数量递减
                latch.countDown();
            }
        }
    }
}
